package com.lanou.pianke.user.web.controller;

import com.lanou.pianke.article.service.ArticleService;
import com.lanou.pianke.piece.web.pieceService.impl.PieceService;
import com.lanou.pianke.user.web.entity.*;
import com.lanou.pianke.user.web.userService.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PieceService pieceService;
    @Autowired
    private ArticleService articleService;


    //用户主页关注粉丝访客和自己所有碎片与文章的显示
    @GetMapping("/pianke/goShowHomepage")
    public void goShowHomepage(Map map,Long uid, Long uid2) {
//        Long uid2 = userService.selectUidByUname(uname);
        if (uid2.equals(uid)){
            User user = userService.selectUserByUid(uid);
            //昵称
            String uname1 = user.getUname();
            map.put("uname", uname1);
            //头像
            String icon = user.getIcon();
            map.put("icon", icon);
            String udesc = user.getUdesc();
            if (udesc == null) {
                udesc = "这是一个还没有简介的Pianker~";
            }
            //简介
            map.put("udesc", udesc);
            //粉丝数
            Integer fansNum = userService.selectUserRelationByUid(uid);
            map.put("fans", fansNum);
            //关注数
            Integer followNum = userService.selectUserRelationFollowByUid(uid);
            map.put("follow", followNum);
            //访客数
            Integer visitorNum = userService.selectVisitorByUid(uid);
            map.put("visitor", visitorNum);
            //用户所有碎片
            List<Piece> piece = userService.selectPieceByUid(uid);
            map.put("oneselfAllPiece", piece);
            //用户所有文章
            List<Articles> articlesList = userService.selectAticlesByUid(uid);
            map.put("oneselfAllArticle", articlesList);
        }else {
            User user = userService.selectUserByUid(uid2);
            //昵称
            String uname1 = user.getUname();
            map.put("uname", uname1);
            //头像
            String icon = user.getIcon();
            map.put("icon", icon);
            String udesc = user.getUdesc();
            if (udesc == null) {
                udesc = "这是一个还没有简介的Pianker~";
            }
            //简介
            map.put("udesc", udesc);
            //粉丝数
            Integer fansNum = userService.selectUserRelationByUid(uid2);
            map.put("fans", fansNum);
            //关注数
            Integer followNum = userService.selectUserRelationFollowByUid(uid2);
            map.put("follow", followNum);
            //访客数
            Integer visitorNum = userService.selectVisitorByUid(uid2);
            map.put("visitor", visitorNum);
            //用户所有碎片
            List<Piece> piece = userService.selectPieceByUid(uid2);
            map.put("oneselfAllPiece", piece);
            //用户所有文章
            List<Articles> articlesList = userService.selectAticlesByUid(uid2);
            map.put("oneselfAllArticle", articlesList);
        }
    }

    //粉丝
    @GetMapping("/pianke/userfan")
    public List<User> userFan(Long uid){
        List<User> list = userService.selectFansByUid(uid);
        return list;
    }

    //关注
    @GetMapping("/pianke/userfollow")
    public List<User> userFollow(Long uid){
        List<User> list = userService.selectFollowByUid(uid);
        return list;
    }

    //访客
    @GetMapping()
    public List<User> userVisitor(Long uid){
        List<User> list = userService.selectAllVisitorByUid(uid);
        return list;
    }

    //进行关注
    @GetMapping("/pianke/follow")
    public void follow(Long authorUid,Long visitorUid){
        userService.insertRelation(authorUid,visitorUid);
    }

    //消息中心显示评论和粉丝
    @GetMapping("/pianke/messageCenter")
    public List<String> messageCenter(Long uid, String uname) {
        List list = userService.selectUserByUname(uid, uname);
        return list;
    }

    //到账号修改界面
    @GetMapping("/pianke/accountSetting")
    public User goAccountSetting(Map map, Long uid) {
        User user = userService.selectUserByUid(uid);
        map.put("updateAccount", user);
        return user;
    }

    //    修改账号
    @GetMapping("/pianke/updateUser")
    public Integer updateAccount(Map map, User user) {
        Integer integer = userService.updateUserAccount(user);
        if (integer != 0) {
            map.put("updateUser", "修改成功");
        } else {
            map.put("updateUser", "昵称已存在！请重新输入昵称");
        }
        return integer;
    }


    //评论通知提示
    @GetMapping("/pianke/commentNotice")
    public Integer commentNotice(Long uid) {
        Integer integer = userService.selectPieceIdByUid(uid);
        Integer commentNumber = articleService.selectCommentsNumber();
        Integer comments = integer + commentNumber;
        return comments;
    }

    //喜欢通知提示
    @GetMapping("/pianke/likeNotice")
    public Integer likeNotice(Long uid) {
        Integer integer = userService.selectPieceLikeByUid(uid);
        return integer;
    }

    //粉丝通知提示
    @GetMapping("/pianke/fanNotice")
    public Integer fanNotice(Long uid) {
        Integer integer = userService.selectUserRelationIdByUid(uid);
        return integer;
    }

    //用户通知里点击喜欢改状态
    @GetMapping("/pianke/clickLike")
    public void clickLike(Long uid){
        userService.updateLikeByUid(uid);
    }


    //用户通知里点击评论改状态
    @GetMapping("/pianke/clickComment")
    public void clickComment(Long uid){
        userService.updatePieceCommentStatusByUid(uid);
    }

    //用户通知里点击粉丝改状态
    @GetMapping("/pianke/clickRelation")
    public void clickRelation(Long uid){
        userService.updateRelationByUid(uid);
    }
}
